package org.jeecg.modules.demo.temple.news.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.temple.news.entity.TempleNews;
import org.jeecg.modules.demo.temple.news.entity.vo.TempleNewsQuery;
import org.jeecg.modules.demo.temple.news.service.ITempleNewsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 新闻
 * @Author: jeecg-boot
 * @Date: 2022-04-21
 * @Version: V1.0
 */
@Api(tags = "新闻")
@RestController
@RequestMapping("/temple.news/templeNews")
@Slf4j
public class TempleNewsController extends JeecgController<TempleNews, ITempleNewsService> {
    @Autowired
    private ITempleNewsService templeNewsService;


    /**
     * @param templeNewsQuery 封装的查询条件
     * @param pageNo          查询页码
     * @param pageSize        每页条数
     * @param req             请求头数
     * @return Result<Page < TempleNews>>  返回值
     * @Description 根据条件分页查询
     * @date 2022/5/26 16:04
     */

    @ApiOperation(value = "新闻-条件列表查询", notes = "新闻-条件列表查询")
    @GetMapping({"/newsQuerysList", "/api/newsQuerysList"})
    public Result<Page<TempleNews>> newsQuerysList(TempleNewsQuery templeNewsQuery,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 判断用户类型
        if (user != null) {
            templeNewsQuery.setSysOrgCode(user.getOrgCode());
        }
        Page<TempleNews> newsPage = new Page<>(pageNo, pageSize);
        // 封装查询条件
        QueryWrapper<TempleNews> wrapper = new QueryWrapper<>();
        //多条件组合查询，动态sql
        String categroyId = templeNewsQuery.getCategroyId();
        String title = templeNewsQuery.getTitle();
        String begin = templeNewsQuery.getBegin();
        String end = templeNewsQuery.getEnd();
        String sysOrgCode= templeNewsQuery.getSysOrgCode() ;
        //判断条件是否为空，拼接条件
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("create_time", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("create_time", end);
        }
        if (!StringUtils.isEmpty(sysOrgCode)) {
            wrapper.eq("sys_org_code", sysOrgCode);
        }

        wrapper.orderByDesc("create_time");

        //调用方法，实现分页查询
        Page<TempleNews> templeNewsPage = templeNewsService.page(newsPage, wrapper);

        return Result.OK(templeNewsPage);
    }

    /**
     * 分页列表查询
     *
     * @param templeNews
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "新闻-分页列表查询")
    @ApiOperation(value = "新闻-分页列表查询", notes = "新闻-分页列表查询")
    @GetMapping({"/list", "/api/list"})
    public Result<IPage<TempleNews>> queryPageList(TempleNews templeNews,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (user != null) {
            templeNews.setSysOrgCode(user.getOrgCode());
        }

        QueryWrapper<TempleNews> queryWrapper = QueryGenerator.initQueryWrapper(templeNews, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<TempleNews> page = new Page<TempleNews>(pageNo, pageSize);
        IPage<TempleNews> pageList = templeNewsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *
     * @Description 根据对象里面的属性值作in查询 属性可能会变 用户组件用到
     * @date 2022/5/26 16:32
     * @param templeNews
     * @return Result<List<TempleNews>>
     */

    @GetMapping("/getMultiNews")
    public Result<List<TempleNews>> getMultiUser(TempleNews templeNews) {
        QueryWrapper<TempleNews> queryWrapper = QueryGenerator.initQueryWrapper(templeNews, null);
        List<TempleNews> ls = this.templeNewsService.list(queryWrapper);

        return Result.OK(ls);
    }

    /**
     * 添加
     *
     * @param templeNews
     * @return
     */
    @AutoLog(value = "新闻-添加")
    @ApiOperation(value = "新闻-添加", notes = "新闻-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody TempleNews templeNews) {
        templeNewsService.save(templeNews);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param templeNews
     * @return
     */
    @AutoLog(value = "新闻-编辑")
    @ApiOperation(value = "新闻-编辑", notes = "新闻-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody TempleNews templeNews) {
        templeNewsService.updateById(templeNews);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "新闻-通过id删除")
    @ApiOperation(value = "新闻-通过id删除", notes = "新闻-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        templeNewsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "新闻-批量删除")
    @ApiOperation(value = "新闻-批量删除", notes = "新闻-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.templeNewsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "新闻-通过id查询")
    @ApiOperation(value = "新闻-通过id查询", notes = "新闻-通过id查询")
    @GetMapping({"/queryById", "/api/queryById"})
    public Result<TempleNews> queryById(@RequestParam(name = "id", required = true) String id) {
        TempleNews templeNews = templeNewsService.getById(id);
        if (templeNews == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(templeNews);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param templeNews
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TempleNews templeNews) {
        return super.exportXls(request, templeNews, TempleNews.class, "新闻");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TempleNews.class);
    }

}
